<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="执业证书编号" prop="officeNumber">
        <el-input
          v-model.trim="queryParams.practiceCertificateNo"
          placeholder="请输入事务所执业证书编号"
          clearable
          size="small"
          class="width220"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事务所名称" prop="firmName">
        <el-input
          v-model.trim="queryParams.firmName"
          placeholder="请输入事务所名称"
          clearable
          size="small"
          class="width220"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="点击进行选择"
          clearable
          size="small"
          class="width220"
          @change="handleQuery"
        >
          <el-option  label="待审核" value="0" />
          <el-option label="通过" value="1" />
          <el-option label="拒绝" value="-2" />
          <el-option label="无需审核" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:post:export']"-->
<!--        >导出</el-button>-->
      </el-form-item>
    </el-form>

    <el-table
      ref="myTable"
      :data="dataList"
      max-height="600"
      border
      stripe
      fit
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column v-if="colData[0].isTrue" fixed label="事务所编号" prop="officeNumber" :render-header="foo"/>
      <el-table-column v-if="colData[1].isTrue" fixed label="事务所名称" prop="firmName" :show-overflow-tooltip="true" :render-header="foo"/>
      <el-table-column v-if="colData[2].isTrue" label="事务所执业证书编号" prop="practiceCertificateNo" :show-overflow-tooltip="true" :render-header="foo"/>
      <el-table-column v-if="colData[3].isTrue" label="统一社会信用代码" prop="unifiedSocialCreditCode" :show-overflow-tooltip="true" :render-header="foo"/>
      <el-table-column v-if="colData[4].isTrue" label="所属省份" prop="province" :render-header="foo"/>
      <el-table-column v-if="colData[5].isTrue" label="所属区县" prop="district" :render-header="foo"/>
      <el-table-column v-if="colData[6].isTrue" label="通讯地址" prop="mailingAddress" :show-overflow-tooltip="true" :render-header="foo"/>
      <el-table-column v-if="colData[7].isTrue" label="联系人" prop="contacts" :render-header="foo"/>
      <el-table-column v-if="colData[8].isTrue" label="联系人手机号" prop="phoneNumberOfContactPerson" :show-overflow-tooltip="true" :render-header="foo"/>
      <el-table-column v-if="colData[10].isTrue" label="社会信用代码证" prop="accountantCertificateFile" width="170" :render-header="foo">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px"
                    v-if="scope.row.accountantCertificateFile != undefined && (scope.row.accountantCertificateFile.indexOf('jpg') !== -1 || scope.row.accountantCertificateFile.indexOf('JPG') !== -1 || scope.row.accountantCertificateFile.indexOf('JPEG') !== -1 || scope.row.accountantCertificateFile.indexOf('jpeg') !== -1  || scope.row.accountantCertificateFile.indexOf('png') !== -1 || scope.row.accountantCertificateFile.indexOf('PNG') !== -1 )"
                    :src="picturePrefix + scope.row.accountantCertificateFile"
                    :preview-src-list="[picturePrefix + scope.row.accountantCertificateFile]" alt="">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <span style="width: 50px; height: 50px" v-if="scope.row.accountantCertificateFile != undefined && ( scope.row.accountantCertificateFile.indexOf('pdf') !== -1 ||scope.row.accountantCertificateFile.indexOf('PDF') !== -1  )" @click="downloadFile(scope.row.accountantCertificateFile,scope.row.firmName+'社会信用代码证')"> <a href="#">{{picturePrefix + scope.row.accountantCertificateFile}}</a></span>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[9].isTrue" label="事务所执业证书" prop="practiceCertificateFile" width="150" :render-header="foo">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px"
                    v-if="scope.row.practiceCertificateFile != undefined && (scope.row.practiceCertificateFile.indexOf('jpg') !== -1 ||  scope.row.practiceCertificateFile.indexOf('JPG') !== -1 || scope.row.practiceCertificateFile.indexOf('JPEG') !== -1 || scope.row.practiceCertificateFile.indexOf('jpeg') !== -1 || scope.row.practiceCertificateFile.indexOf('png') !== -1 || scope.row.practiceCertificateFile.indexOf('PNG') !== -1 )"
                    :src="picturePrefix + scope.row.practiceCertificateFile"
                    :preview-src-list="[picturePrefix + scope.row.practiceCertificateFile]" alt="">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <span style="width: 50px; height: 50px" v-if="scope.row.practiceCertificateFile != undefined && (scope.row.practiceCertificateFile.indexOf('pdf') !== -1 || scope.row.practiceCertificateFile.indexOf('PDF') !== -1 ) " @click="downloadFile(scope.row.practiceCertificateFile,scope.row.firmName+'事务所执业证书')"> <a href="#">{{picturePrefix + scope.row.practiceCertificateFile}}</a></span>
        </template>
      </el-table-column>
     <!-- <el-table-column v-if="colData[13].isTrue" label="签字注会1证书" prop="registerCertificateOneFile" width="300" :render-header="foo">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px"
                    v-if="scope.row.registerCertificateOneFile != undefined && (scope.row.registerCertificateOneFile.indexOf('jpg') !== -1 || scope.row.registerCertificateOneFile.indexOf('png') !== -1 )"
                    :src="picturePrefix + scope.row.registerCertificateOneFile"
                    :preview-src-list="[picturePrefix + scope.row.registerCertificateOneFile]" alt="">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
          <span style="width: 50px; height: 50px" v-if="scope.row.registerCertificateOneFile != undefined && scope.row.registerCertificateOneFile.indexOf('pdf') !== -1 "> {{picturePrefix + scope.row.registerCertificateOneFile}}</span>
        </template>
      </el-table-column>

      <el-table-column v-if="colData[14].isTrue" label="签字注会2证书" prop="registerCertificateTwoFile" width="300" :render-header="foo">
      <template slot-scope="scope">
        <el-image style="width: 50px; height: 50px"
                  v-if="scope.row.registerCertificateTwoFile != undefined && (scope.row.registerCertificateTwoFile.indexOf('jpg') !== -1 || scope.row.registerCertificateTwoFile.indexOf('png') !== -1 )"
                  :src="picturePrefix + scope.row.registerCertificateTwoFile"
                  :preview-src-list="[picturePrefix + scope.row.registerCertificateTwoFile]" alt="">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline"></i>
          </div>
        </el-image>
        <span style="width: 50px; height: 50px" v-if="scope.row.registerCertificateTwoFile != undefined && scope.row.registerCertificateTwoFile.indexOf('pdf') !== -1 "> {{picturePrefix + scope.row.registerCertificateTwoFile}}</span>
      </template>
      </el-table-column>-->
<!--   '0：待审核，1：审核通过，-2：审核不通过，2：无需审核',-->
      <el-table-column v-if="colData[11].isTrue" label="审核状态" prop="auditStatus" :render-header="foo">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.auditStatus && scope.row.auditStatus === -2 ? 'danger': 'success'"
            disable-transitions>{{formatterAuditStatus(scope.row)}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column v-if="colData[12].isTrue" label="操作" fixed="right" width="160">
        <template slot-scope="scope">
          <el-button v-if="scope.row.auditStatus === 0" plain size="mini" @click="handleUpdate(scope.row)" v-hasPermi="['office:examine:audit']">审核</el-button>
          <el-button plain size="mini" @click="handleViewInfo(scope.row)" >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :autoScroll="false"
      @pagination="getList">
      <template v-slot:popover>
        <el-popover
          placement="top-end"
          width="260"
          trigger="hover"
        >
          <!--弹出的菜单内容-->
          <div class="selectMenu">
            <el-checkbox-group v-model="colOptions">
              <el-checkbox v-for="item in colSelect" :key="item" :label="item" class="displayBlock marginBottom10"/>
            </el-checkbox-group>
          </div>
          <el-button slot="reference" size="mini" class="selectBtn">
            <i class="el-icon-menu"></i><i class="el-icon-caret-bottom"></i>
          </el-button>
        </el-popover>
      </template>
    </pagination>

    <!--    事务所导入成功 提示框-->
    <el-dialog
      title="事务所导入成功"
      :visible.sync="dialogVisible"
      width="360px"
      append-to-body
      center
    >
      <div class="textAlignCenter">
        <img src="@/assets/image/success.png" alt="" width="110" height="100" class="displayBlock marginBottom20 marginAuto">
        <p>导入成功，更新 36 条数据，新增 2 条数据</p>
        <h4 class="fontSize20 marginTop20">点击任何地方关闭弹窗</h4>
      </div>
    </el-dialog>
    <!--    事务所导入中-->
    <el-dialog
      title="事务所导入中..."
      :visible.sync="progressVisible"
      width="360px"
      :close-on-click-modal="false"
      :show-close="false"
      append-to-body
      center
    >
      <div class="textAlignCenter">
        <img src="@/assets/image/import.png" alt="" width="300" height="150" class="displayBlock marginBottom20 marginAuto">
        <p>正在进行数据比对，请耐心等待...</p>
        <h4 class="fontSize20 marginTop20">75 %</h4>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog
      title="审核"
      :visible.sync="dialogVisibleCheck"
      width="418px"
      append-to-body
    >
      <div class="dialogView">
        <el-form :model="checkForm" :rules="checkRules" ref="checkForm">
          <el-row :gutter="20">
            <el-col :span="24" class="form-wrap">
              <el-form-item label="状态:" prop="auditStatus"  class="from-group">
                <el-radio-group v-model="checkForm.auditStatus"  @change="updateRules">
                  <el-radio :label="1">通过</el-radio>
                  <el-radio :label="-2">不通过</el-radio>
                </el-radio-group>
              </el-form-item>

              <el-form-item label="账号结束日期:" prop="effectTime" v-if="checkForm.auditStatus == '1'">
                <el-date-picker
                  v-model="checkForm.effectTime"
                  size="small"
                  class="endDate"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="请选择日期"
                  clearable
                ></el-date-picker>
              </el-form-item>

              <el-form-item label="理由:" prop="optReason" class="from-group">
                <el-input
                  type="textarea"
                  v-model="checkForm.optReason"
                  placeholder="请输入审核理由"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>

            </el-col>
          </el-row>
          <div class="flexRowBetween">
            <el-button type="primary" style="flex: 1;" @click="handleCheckForm">发送</el-button>
            <el-button style="flex: 1;" @click="publicHandleCancel('dialogVisibleCheck')">取消</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
    <!-- end -->
  </div>
</template>

<script>
  import { listAuditOffice, addOffice, checkOffice } from "@/api/system/office";
  export default {
    name: "OfficeExamineList",
    data() {
      const containSpecial = RegExp(
          /[(\)(\~)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\|)(\\)(\/)(\<)(\>)(\)]+/
      );
      const validateInput = (rule, value, callback) => {
        if(value === ''){
          callback(new Error("审核理由不能为空"))
        }else if(containSpecial.test(value)) {
            return callback(new Error("不能包含非法字符"));
        } else {
            callback();
        }
      };
      return {
        dialogVisible: false,  // 弹框： 导入成功
        progressVisible: false,  // 弹框： 导入中
        dialogVisibleCheck: false, // 弹框： 审核
        picturePrefix: process.env.VUE_APP_NGINX_STATIC_RESOURCE_MAPPING_URL, // 图片前缀
        dataList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          practiceCertificateNo: '', // 事务所执业证书编号
          auditStatus: '待审核', // 审核状态
          firmName: '', // 事务所名称 100
        },
        // 多选框的可选择项
        colOptions: ['事务所编号', '事务所名称', '事务所执业证书编号', '统一社会信用代码', '所属省份', '所属区县', '通讯地址', '联系人', '联系人手机号', '事务所执业证书', '社会信用代码证',"签字注会1证书","签字注会2证书", '审核状态', '操作' ],
        // 多选框 已选择 的内容，即表格中显示的列
        colSelect: ['事务所编号', '事务所名称', '事务所执业证书编号', '统一社会信用代码', '所属省份', '所属区县', '通讯地址', '联系人', '联系人手机号', '事务所执业证书', '社会信用代码证',"签字注会1证书","签字注会2证书", '审核状态', '操作' ],
        // isTrue属性存放列的状态
        colData: [
          { title: '事务所编号', isTrue: true },
          { title: '事务所名称', isTrue: true },
          { title: '事务所执业证书编号', isTrue: false },
          { title: '统一社会信用代码', isTrue: true },
          { title: '所属省份', isTrue: true },
          { title: '所属区县', isTrue: false },
          { title: '通讯地址', isTrue: false },
          { title: '联系人', isTrue: true },
          { title: '联系人手机号', isTrue: true },
          { title: '事务所执业证书', isTrue: true },
          { title: '社会信用代码证', isTrue: true },
          { title: '审核状态', isTrue: true },
          { title: '操作', isTrue: true },
          { title: '签字注会1证书', isTrue: true },
          { title: '签字注会2证书', isTrue: true },
        ],
        // 审核表单
        checkForm: {
            auditStatus:-2, // 审核状态
            optReason: '', // 审核理由
            effectTime:''
        },
        checkRules: {
            auditStatus: [
                {required: true },
            ],
            optReason: [
                {required: true, message: "审核理由不能为空",trigger: "blur",},
                {min: 1,validator: validateInput, trigger: 'blur'}
            ]
        },
      };
    },
    beforeUpdate() {
      // 防止动态添加列时 表格乱
      this.$nextTick(() => {
        this.$refs.myTable.doLayout();
      });
    },
    watch: {
      colOptions(newVal, oldVal) {
        if (newVal) {    // 如果有值发生变化，即多选框的已选项变化
          let arr = this.colSelect.filter(i => newVal.indexOf(i) < 0) 	// 未选中
          this.colData.filter(i => {
            if (arr.indexOf(i.title) !== -1) {
              i.isTrue = false
            } else {
              i.isTrue = true
            }
          })
        }
      }
    },
    created() {
      this.colOptions = [];
      this.colData.forEach(item => {
          if(item.isTrue){
              this.colOptions.push(item.title);
          }
      });
      if(this.$route.query && this.$route.query.practiceCertificateNo){
        this.queryParams = {};
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          practiceCertificateNo:this.$route.query.practiceCertificateNo,
        }
        this.getList()
      }else{
        this.getList();
      }
    },
    methods: {
      foo(h,{column}) {
          return this.utilsFoo(h,{column})
      },
      /** 查询权限列表 */
      getList() {
        this.loading = true;
        let obj = JSON.stringify(this.queryParams);
        let init = JSON.parse(obj);
        if(init.auditStatus === '待审核'){
          init.auditStatus = 0
        }
        listAuditOffice(init).then(
          response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      //  查看详情
      handleViewInfo(row){
         this.$router.push({ path: '/office/examine/detail',query:{ officeId: row.officeId }});
      },
      downloadFile(fileId,fileName){
        window.location.href=process.env.VUE_APP_BASE_API +"/ftp/fileDownload?fileId="+fileId+"&fileName="+fileName+".pdf";
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roleId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.approvedPracticeDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleAdd() {
        this.$router.push({ path: '/office/edit'})
      },
      handleDelete() {

      },
      handleImport() {

      },
      handleExport() {

      },
      publicHandleCancel(str){
          this[str] = false;
      },
        //  打开审核界面
      handleUpdate(row) {
        this.dialogVisibleCheck = true;
        this.checkForm.officeId = row.officeId;
        this.checkForm.optReason = "已审核";
        this.checkForm.effectTime = this.getAfterFormatDate(5)
      },
      updateRules(){
        let self = this
        if(self.checkForm.auditStatus === 1){
          self.checkRules.optReason=[{required: false, trigger: "blur", message: "理由不能为空"}];
        }else{
          self.checkRules.optReason=[{required: true, trigger: "blur", message: "理由不能为空"}];
        }
      },
        // 待审核
      handleCheckForm(){
        let self = this
        self.$refs.checkForm.validate(valid => {
            if (valid) {
                self.loading = true;
                 checkOffice(self.checkForm).then(res=>{
                    if(res.code === 200){
                        self.dialogVisibleCheck = false;
                           self.loading = false;
                      this.$message({
                        type: 'success',
                        message: '审核提交成功!'
                      });
                         self.getList();
                    }

                }).catch(()=>{
                   self.loading = false;
               });
            }
        });
      },
      formatterAuditStatus(row){
        return row.auditStatus == '-2'? '拒绝' : row.auditStatus == '1'? '通过': row.auditStatus == '2'? '无需审核':'待审核';
      },
    }
  };
</script>
<style lang="scss" scoped>
  .app-container{
    position: relative;
  }
  /*form 表单 样式设置 start*/
  ::v-deep .el-form-item{
    margin-bottom: 10px;
  }
  ::v-deep label[for='unifiedSocialCreditCode'],
  ::v-deep label[for='nameOfChargePerson'],
  ::v-deep label[for='certificateNoOfResponsity']{
    line-height: 20px;
  }
  ::v-deep .el-input--suffix .el-input__inner {
    padding-right: 10px;
  }
  ::v-deep .el-select .el-input--suffix .el-input__inner {
    padding-right: 30px;
  }
  ::v-deep .el-date-editor .el-range-input{
    width: 62%;
  }
  ::v-deep .el-dialog__body{
    padding-top:10px;
  }
  /*form 表单 样式设置 end*/

  .width220{
    width: 220px;
  }
  .failedContent{
    margin-top: 20px;
    padding: 20px;
    border: 1px dashed #ccc;
    p{
      line-height: 24px;
    }
  }
  .from-group{
    display: flex;
    width: 100%;
  }
  .flexRowBetween{
    margin-top:20px;
  }
  ::v-deep .el-form-item__content{
    flex:1;
  }
  ::v-deep .el-image-viewer__btn{
    color:#000;
  }
  .endDate{
    width: calc(100% - 100px) !important;
  }
  ::v-deep .el-form-item__label{
    width:100px;
  }
</style>
